package com.ddwl.shop.service;

import com.ddwl.common.exception.CommonException;
import com.ddwl.common.exception.ExceptionCode;
import com.ddwl.common.util.ObjectUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.shop.MerchantBo;
import com.ddwl.schema.bo.shop.MerchantSearchBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.shop.MerchantVo;
import com.ddwl.shop.dao.mapper.MerchantMapper;
import com.ddwl.shop.dao.model.Merchant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户服务
 *
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/8/29 17:13
 */
@Service
@RequiredArgsConstructor
public class MerchantService {
    private final MerchantMapper merchantMapper;

    public MerchantVo detail(String uid) {
        Merchant entity = merchantMapper.selectByPrimaryKey(uid);
        if(entity == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        MerchantVo vo = new MerchantVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Transactional(rollbackFor = Throwable.class)
    public void create(MerchantBo bo) {
        Merchant entity = new Merchant();
        BeanUtils.copyProperties(bo, entity);
        entity.setId(UUIDGenerator.getShortUUID());
        entity.setAuditStatus((byte)0);
        entity.setDflag(false);
        merchantMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(MerchantBo bo) {
        Merchant entity = merchantMapper.selectByPrimaryKey(bo.getId());
        if (entity == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        BeanUtils.copyProperties(bo, entity, ObjectUtil.getNullFields(bo));
        merchantMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void delete(String id) {
        Merchant merchant = new Merchant();
        merchant.setId(id);
        merchant.setDflag(true);
        merchantMapper.updateByPrimaryKeySelective(merchant);
    }

    public PageVo<MerchantVo> page(PageBo<MerchantSearchBo> pageBo) {
        PageVo<MerchantVo> pageVo = new PageVo<>();
        BeanUtils.copyProperties(pageBo, pageVo);
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize());
        Example example = new Example(Merchant.class);
        example.and().andEqualTo("dflag", false);
        if(pageBo.getParam() != null) {
            if(StringUtils.hasText(pageBo.getParam().getKeyword())) {
                example.and().andLike("name", pageBo.getParam().getKeyword());
            }
        }
        Page<Merchant> pageable = (Page<Merchant>) merchantMapper.selectByExample(example);
        pageVo.setTotal(pageable.getTotal());
        pageVo.setData(pageable.getResult().stream().map(entity -> {
            MerchantVo vo = new MerchantVo();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList()));
        return pageVo;
    }

    public List<MerchantVo> search(MerchantSearchBo bo) {
        PageHelper.startPage(1, 20, false);
        Example example = new Example(Merchant.class);
        example.and().andEqualTo("dflag", false);
        if(StringUtils.hasText(bo.getKeyword())) {
            example.and().andLike("name", bo.getKeyword());
        }
        List<Merchant> entities = merchantMapper.selectByExample(example);
        return entities.stream().map(entity -> {
            MerchantVo vo = new MerchantVo();
            BeanUtils.copyProperties(entity, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
